package com.example.service.impl;

import com.example.mapper.precisionMeasProjectMapp;
import com.example.pojo.precisionMeasProjectPojo;
import com.example.pojo.precisionMeasResultPojo;
import com.example.service.precisionMeasProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * ClassName:precisionMeasProjectImpl
 * Description:
 *
 * @Author 赵永峰
 * @Create 2024/12/30 15:32
 * @Version 1.0
 */
@Service
public class precisionMeasProjectImpl implements precisionMeasProjectService {
    //注入Mapper层
    @Autowired
    private precisionMeasProjectMapp precisionMeasProjectMapp;
//    插入一行
    public void insertOne(precisionMeasProjectPojo precisionMeasProjectPojo){
        //生成一段低代码格式的id
        UUID uuid = UUID.randomUUID();
        String uuidStr = uuid.toString().replace("-", "");
        precisionMeasProjectPojo.setId(uuidStr);
        //定义创建时间生成规则
        precisionMeasProjectPojo.setCreate_time(LocalDateTime.now());
        //定义修改时间生成规则
        precisionMeasProjectPojo.setUpdate_time(LocalDateTime.now());
        precisionMeasProjectMapp.insertOne(precisionMeasProjectPojo);
    }
    //输入三个字段，进行查询,适用查询的是三个字符串格式的数据
    public List<precisionMeasProjectPojo> inquiryThreeField(Map<String, Object> map){
        String firstFieldName=(String) map.get("firstFieldName");
        String firstFieldVal=(String) map.get("firstFieldVal");
        String secondFieldName=(String) map.get("secondFieldName");
        String secondFieldVal=(String) map.get("secondFieldVal");
        String thirdName=(String) map.get("thirdName");
        String thirdVal=(String) map.get("thirdVal");
        List<precisionMeasProjectPojo> precisionMeasProjectPojoList =precisionMeasProjectMapp.inquiryThreeField(firstFieldName,firstFieldVal,secondFieldName,secondFieldVal,thirdName,thirdVal);
        return precisionMeasProjectPojoList;
    }
    //输入四个字段，进行查询
    public List<precisionMeasProjectPojo> inquiryFourthField(Map<String, Object> map){
        String firstFieldName=(String) map.get("firstFieldName");
        String firstFieldVal=(String) map.get("firstFieldVal");
        String secondFieldName=(String) map.get("secondFieldName");
        String secondFieldVal=(String) map.get("secondFieldVal");
        String thirdName=(String) map.get("thirdName");
        String thirdVal=(String) map.get("thirdVal");
        String fourthFieldName=(String) map.get("fourthFieldName");
        String fourthFiledVal=(String) map.get("fourthFiledVal");
        List<precisionMeasProjectPojo> precisionMeasProjectPojoList =precisionMeasProjectMapp.inquiryFourthField(firstFieldName,firstFieldVal,secondFieldName,secondFieldVal,thirdName,thirdVal,fourthFieldName,fourthFiledVal);
        return precisionMeasProjectPojoList;
    }
    //一次插入多行数据
    public void testValPrecisionMeasProject(List<precisionMeasResultPojo> precisionMeasResultPojos){
        for (int i = 0; i < precisionMeasResultPojos.size(); i++) {
            //生成一段低代码格式的id
            UUID uuid = UUID.randomUUID();
            String uuidStr = uuid.toString().replace("-", "");
            precisionMeasResultPojos.get(i).setId(uuidStr);
            //设置创立时间
            precisionMeasResultPojos.get(i).setUpdate_time(LocalDateTime.now());
            precisionMeasResultPojos.get(i).setCreate_time(LocalDateTime.now());
        }
        precisionMeasProjectMapp.testValPrecisionMeasProject(precisionMeasResultPojos);
    }
    //根据id，修改一整行。
    public void precisionMeasProjectEditOne(precisionMeasProjectPojo precisionMeasProjectPojo){
        //组合修改时间
        precisionMeasProjectPojo.setUpdate_time(LocalDateTime.now());
        //向修改数据
        String id = precisionMeasProjectPojo.getId();
        precisionMeasProjectMapp.editOne(id, precisionMeasProjectPojo);
    }
    //修改一个字段.依据id,修改的是int格式
    public void precisionMeasProjectEditOneField(Map<String, Object> map){
        System.out.println(map);
        String id = (String) map.get("id");
        String firstFieldName = (String) map.get("firstFieldName");
        Integer firstFieldVal = (Integer) map.get("firstFieldVal");
        System.out.println(firstFieldVal);
        LocalDateTime update_time=LocalDateTime.now();
        precisionMeasProjectMapp.oneField(id,firstFieldName,firstFieldVal,update_time);
    }
    //接收5个参数，查询出检测结果。
    public List<precisionMeasResultPojo> precisionMeasProjectValInquiryFiveField(Map<String, Object> map){
        String firstFieldName=(String) map.get("firstFieldName");
        String firstFieldVal=(String) map.get("firstFieldVal");
        String secondFieldName=(String) map.get("secondFieldName");
        String secondFieldVal=(String) map.get("secondFieldVal");
        String thirdName=(String) map.get("thirdName");
        String thirdVal=(String) map.get("thirdVal");
        String fourthName=(String) map.get("fourthName");
        String fourthVal=(String) map.get("fourthVal");
        String fifthName=(String) map.get("fifthName");
        int fifthVal=(Integer) map.get("fifthVal");

        if (fourthName.equals("nothing")){
            //没有特性值，按照三个特性和一个最近数量筛选
            List<precisionMeasResultPojo> precisionMeasResultPojos =precisionMeasProjectMapp.precisionMeasProjectValInquiryFourField(firstFieldName,firstFieldVal,secondFieldName,secondFieldVal,thirdName,thirdVal,fifthVal);
            return precisionMeasResultPojos;
        }
        else {
            List<precisionMeasResultPojo> precisionMeasResultPojos =precisionMeasProjectMapp.precisionMeasProjectValInquiryFiveField(firstFieldName,firstFieldVal,secondFieldName,secondFieldVal,thirdName,thirdVal,fourthName,fourthVal, fifthVal);
            return precisionMeasResultPojos;
        }
    }
    //批量的插入检测标准数据
    public void insertTableDataList(List<precisionMeasProjectPojo> pojos){
        for (int i = 0; i < pojos.size(); i++) {
            //生成一段低代码格式的id
            UUID uuid = UUID.randomUUID();
            String uuidStr = uuid.toString().replace("-", "");
            pojos.get(i).setId(uuidStr);
            pojos.get(i).setUpdate_time(LocalDateTime.now());
            pojos.get(i).setCreate_time(LocalDateTime.now());
        }
        precisionMeasProjectMapp.insertTableDataList(pojos);
    }

    //查询检测结果表的某字段为某值的数据，分页查询
    public List<precisionMeasResultPojo> queryFourField(Map<String, Object> map){
        Integer page = (Integer) map.get("page");
        Integer size = (Integer) map.get("size");
        String firstName = map.get("firstName").toString();
        String firstValue = map.get("firstValue").toString();
        String secondName = map.get("secondName").toString();
        String secondValue = map.get("secondValue").toString();
        String thirdName = map.get("thirdName").toString();
        String thirdValue = map.get("thirdValue").toString();
        String forthName = map.get("forthName").toString();
        String forthValue = map.get("forthValue").toString();
        Integer startRow=page*size;
        List<precisionMeasResultPojo> pojoList = precisionMeasProjectMapp.queryFourField(startRow, size, firstName, firstValue, secondName, secondValue, thirdName, thirdValue, forthName, forthValue);
        return pojoList;
    }

    //查询检测结果总条数
    public Integer queryQuantity(Map<String, Object> map){
        String firstName = map.get("firstName").toString();
        String firstValue = map.get("firstValue").toString();
        String secondName = map.get("secondName").toString();
        String secondValue = map.get("secondValue").toString();
        String thirdName = map.get("thirdName").toString();
        String thirdValue = map.get("thirdValue").toString();
        String forthName = map.get("forthName").toString();
        String forthValue = map.get("forthValue").toString();
        Integer quantity = precisionMeasProjectMapp.queryQuantity(firstName, firstValue, secondName, secondValue, thirdName, thirdValue, forthName, forthValue);
        return quantity;
    }

    //动态查询检测结果
    public List<precisionMeasResultPojo> queryByDynamicConditions(Map<String, Object> map){
        return precisionMeasProjectMapp.queryByDynamicConditions(map);
    }
    public Integer queryCountByDynamicConditions(Map<String, Object> map){
        return precisionMeasProjectMapp.queryCountByDynamicConditions(map);
    }

    //编辑某一行测量结果
    public void editOneRow(precisionMeasResultPojo precisionMeasResultPojo){
        precisionMeasResultPojo.setUpdate_time(LocalDateTime.now());
        String id = precisionMeasResultPojo.getId();
        precisionMeasProjectMapp.editOneRow(id,precisionMeasResultPojo);
    }

    //根据检测项目查询指定数量的数据
    public List<precisionMeasResultPojo> queryByOneField(Map<String, Object> map){
        Integer quantity = (Integer) map.get("quantity");
        String measuring_project_id = map.get("measuring_project_id").toString();
        List<precisionMeasResultPojo> pojoList = precisionMeasProjectMapp.queryByOneField(quantity, measuring_project_id);
        return pojoList;
    }
}
